//
//  Drop+CoreDataClass.swift
//  Drop
//
//  Created by hushaohua on 2017/12/19.
//  Copyright © 2017年 init. All rights reserved.
//
//

import Foundation
import CoreData

@objc(Drop)
public class Drop: NSManagedObject {
    var context:NSManagedObjectContext?;
    static func newInstance() -> Drop{
        let context : NSManagedObjectContext = HStoreManager.defaultManger().context;
        let drop:Drop = NSEntityDescription.insertNewObject(forEntityName: "Drop", into: context) as! Drop;
        return drop;
    }
    
    func deleteFromContext() -> Void{
        let context : NSManagedObjectContext = HStoreManager.defaultManger().context;
        context.delete(self);
    }
    
    static func defaultFetchRequest() -> NSFetchRequest<Drop>{
        return NSFetchRequest(entityName: "Drop");
    }
    
    static func defaultSort() -> NSSortDescriptor{
        return NSSortDescriptor(key: "begin", ascending: false);
    }
    
    static func fetchInstancesFrom(_ request:NSFetchRequest<Drop>) -> [Drop]?{
        do{
            let drops = try HStoreManager.defaultManger().context.fetch(request);
            return drops;
        }catch{
            return nil;
        }
    }
    
    public override func didChangeValue(forKey key: String) {
        if (key == "begin"){
            self.configDateComponents();
        }
        super.didChangeValue(forKey: key);
    }
    
    var hideNote:Bool = true;
    
}
